<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('商品编码')">
              <j-input :placeholder="$t('请输入商品编码')" v-model="queryParam.skuId"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('商品名称')">
              <j-input :placeholder="$t('请输入商品名称')" v-model="queryParam.skuName"></j-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('商品分类')">
                <a-tree-select
                  allowClear
                  v-model="queryParam.category"
                  :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="treeData"
                  :placeholder="$t('请输入商品分类')"
                  :load-data="onLoadData"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('上架状态')">
                <a-select v-model="queryParam.skuState">
                  <a-select-option v-for="item in dictOptions.state" :key="item.value" :value="item.value">{{
                    item.text
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item>
                <div>
                  <a-select v-model="queryParam.newType" style="width: 29%; margin-right: 1%">
                    <a-select-option v-for="(item, i) in dictOptions.typeList" :key="i" :value="item.value">{{
                      $t(item.text)
                    }}</a-select-option>
                  </a-select>
                  <a-input-number v-model="queryParam.newValue_begin" style="width: 32%; margin-right: 1%" />
                  <span>~</span>
                  <a-input-number v-model="queryParam.newValue_end" style="width: 32%; margin-left: 1%" />
                </div>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator">
      <a-button type="primary" @click="batchUp">{{ $t('批量上架') }}</a-button>
      <a-button type="primary" @click="batchDown" style="margin-left: 8px">{{ $t('批量下架') }}</a-button>
    </div> -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{ $t('编辑') }}</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">{{ $t('更多') }} <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">{{ $t('详情') }}</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)">
                  <a>{{ $t('删除') }}</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <bis-goods-infor-modal ref="modalForm" @ok="modalFormOk"></bis-goods-infor-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { putAction, deleteAction, getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import BisGoodsInforModal from './modules/BisGoodsInforModal'

export default {
  name: 'JDGoodsInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    BisGoodsInforModal,
  },
  data() {
    return {
      description: '京东管理页面',
      // 表头
      columns: [
        {
          title: this.$t('商品编码'),
          align: 'center',
          dataIndex: 'skuId',
        },
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'skuName',
        },
        {
          title: this.$t('商品分类'),
          align: 'center',
          dataIndex: 'category',
        },
        {
          title: this.$t('上架状态'),
          align: 'center',
          dataIndex: 'skuState',
          customRender: (text) => filterMultiDictText(this.dictOptions['state'], text),
        },
        {
          title: this.$t('售价'),
          align: 'center',
          dataIndex: 'salePrice',
        },
        {
          title: this.$t('代币兑换值'),
          align: 'center',
          dataIndex: 'newCoin',
        },
        {
          title: this.$t('彩票兑换值'),
          align: 'center',
          dataIndex: 'newTicket',
        },
        {
          title: this.$t('积分兑换值'),
          align: 'center',
          dataIndex: 'newIntegral',
        },
        {
          title: this.$t('A币兑换值'),
          align: 'center',
          dataIndex: 'newAcoin',
        },
        {
          title: this.$t('A票兑换值'),
          align: 'center',
          dataIndex: 'newAticket',
        },
      ],
      url: {
        list: '/gift/jdGoodsInfo',
      },
      dictOptions: {
        typeList: [],
      },
      superFieldList: [],
      treeData: [],
      allChildKind: [],
      queryParam: {
        newType: '0',
      },
    }
  },
  created() {
    this.$set(this.dictOptions, 'state', [
      { text: this.$t('是'), value: '1' },
      { text: this.$t('否'), value: '0' },
    ])
    //this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    searchReset() {
      this.queryParam = {
        newType: '0',
      }
      this.searchQuery()
    },
    initDictConfig() {
      this.select()
      getAction('/gift/dict', { dictCode: 'bis_goods_infor.new_type' }).then((res) => {
        if (res.success) {
          this.$set(this.dictOptions, 'typeList', res.result)
        }
      })
    },
    batchUp() {
      let that = this
      if (that.selectedRowKeys.length <= 0) {
        that.$message.warning(that.$t('请先选择要操作的商品'))
        return
      } else {
        let ids = that.selectedRowKeys.join(',')
        that.$confirm({
          title: that.$t('确认'),
          content: that.$t('您确定要对已选择的商品进行上架操作吗?'),
          okText: that.$t("确定"),
          cancelText: that.$t("取消"),
          onOk: function () {
            that.loading = true
            putAction(that.url.listBatch, { ids: ids, state: 'Y' })
              .then((res) => {
                if (res.success) {
                  that.loadData(1)
                  that.$message.success(res.message)
                  that.selectedRowKeys = []
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          },
        })
      }
    },
    batchDown() {
      let that = this
      if (that.selectedRowKeys.length <= 0) {
        that.$message.warning(that.$t('请先选择要操作的商品'))
        return
      } else {
        let ids = that.selectedRowKeys.join(',')
        that.$confirm({
          title: that.$t('确认'),
          content: that.$t('您确定要对已选择的商品进行下架操作吗?'),
          okText: that.$t("确定"),
          cancelText: that.$t("取消"),
          onOk: function () {
            that.loading = true
            putAction(that.url.listBatch, { ids: ids, state: 'N' })
              .then((res) => {
                if (res.success) {
                  that.loadData(1)
                  that.$message.success(res.message)
                  that.selectedRowKeys = []
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          },
        })
      }
    },
    batchDel() {
      let that = this
      if (that.selectedRowKeys.length <= 0) {
        that.$message.warning(that.$t('请先选择要操作的商品'))
        return
      } else {
        let ids = that.selectedRowKeys.join(',')
        that.$confirm({
          title: that.$t('确认'),
          content: that.$t('您确定要对已选择的商品进行删除操作吗?'),
          okText: that.$t("确定"),
          cancelText: that.$t("取消"),
          onOk: function () {
            that.loading = true
            deleteAction(that.url.deleteBatch, { ids: ids })
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.loadData(1)
                  that.selectedRowKeys = []
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          },
        })
      }
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'orderSort'
      param.order = 'asc'
      getAction('/gift/jdGoodsKind', param).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.categoryName
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                col.isLeaf = false
                col.children = []
              } else {
                col.isLeaf = true
              }
            })
            that.treeData = res.result.records
          } else {
            that.treeData = []
          }
        } else {
          that.$alert(res.message, '提示', {
            confirmButtonText: '确定',
            callback: (action) => {},
          })
        }
      })
    },
    onLoadData(treeNode) {
      let that=this
      const { id } = treeNode.dataRef
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = id
      return getAction('/gift/jdGoodsKind', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.categoryName
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                col.isLeaf = false
              } else {
                col.isLeaf = true
              }
            })
            //record.children = that.getDataByResult(res.result.records)
            treeNode.dataRef.children = res.result.records
            that.treeData = [...that.treeData]
          }
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>